
/*
Component:worker and socket - worker
author:lhm
time:2017.07.19
 */

//obj 对象
var workerObj_normal;
var workerObj_save;
var workerObj_update;

var ipAddress_normal = "ws://10.1.21.80:1234";//实际连接 xu
var ipAddress_save = "ws://10.1.21.80:1234";
var ipAddress_update = "ws://10.1.21.80:1234";

// var ipAddress_normal = "ws://10.1.21.231:1234";//实际连接 peng
// var ipAddress_save = "ws://10.1.21.231:1234";
// var ipAddress_update = "ws://10.1.21.231:1234";

function start_worker(type)
{
	switch(type)
	{
	case 'normal':
		if(typeof(Worker)!=="undefined"){
			if(typeof(workerObj_normal)=="undefined"){
				workerObj_normal=new Worker("socket/worker_socket.js");
				// console.log("workerObj_normal open");
				log("workerObj_normal open");
			}
		}
		else{
			console.log("this browser not support worker");
		}
		break;
	case 'save':
		if(typeof(Worker)!=="undefined"){
			if(typeof(workerObj_save)=="undefined"){
				workerObj_save=new Worker("socket/worker_socket.js");
				// console.log("workerObj_save open");
				log("workerObj_save open");
			}
		}
		else{
			console.log("this browser not support worker");
		}
		break;
	case 'update':
		if(typeof(Worker)!=="undefined"){
			if(typeof(workerObj_update)=="undefined"){
				workerObj_update=new Worker("socket/worker_socket.js");
				// console.log("workerObj_update open");
				log("workerObj_update open");
			}
		}
		else{
			console.log("this browser not support worker");
		}
		break;
	default:
		console.log("wrong order");
	}
}

function stop_worker(type)
{
	switch(type)
	{
	case 'normal':
		workerObj_normal.terminate();
		workerObj_normal = null;
		// console.log("workerObj_normal close");
		log("workerObj_save close");
		break;
	case 'save':
		workerObj_save.terminate();
		workerObj_save = null;
		// console.log("workerObj_save close");
		log("workerObj_save close");
		break;
	case 'update':
		workerObj_update.terminate();
		workerObj_update = null;
		// console.log("workerObj_update close");
		log("workerObj_save close");
		break;
	default:
		//return false;
	}
}

function worker_normal_recv_init()//返回
{
	workerObj_normal.onmessage = function (event) {
		console.log("from worker normal : "+ event.data);

		// log("from worker normal : "+ event.data);
		//实际处理写在下方
		var cmd = event.data.substring(0,8);//解决存在多个冒号的问题
		var command = event.data.substring(9,event.data.length);

		switch(cmd)
		{
		case 'connect_':
			if(command == "open")//刚连接 启动成功	
			{
				console.log("from worker normal : "+ event.data);
				log("from worker normal : open");
				workerObj_normal_state = true;
			}
			else//连接失败
			{
				console.log("from worker normal : "+ event.data);
				log("from worker normal : close");
				workerObj_normal_state = false;
			}
			break;
		case 'login___':
			log("from worker normal : " + command);
			//data_login = true;//这里要做判断 是否登陆成功
			break;
		case 'getMsg__'://消息获取
			data_message = command;
			break;
		default:
			//return false;
			log("from worker normal : "+ event.data);
		}
	};

	//初始化ip
	workerObj_normal.postMessage(ipAddress_normal); 
}

function worker_save_recv_init()//返回
{
	workerObj_save.onmessage = function (event) {
		console.log("from worker save : "+ event.data);
		// log("from worker save : "+ event.data);
		//实际处理写在下方
	};

	//初始化ip
	workerObj_save.postMessage(ipAddress_save); 
}

function worker_update_recv_init()//返回
{
	workerObj_update.onmessage = function (event) {
		// console.log("from worker update : "+ event.data);
		// log("from worker update : "+ event.data);
		// 实际处理写在下方

		var cmd = event.data.substring(0,8);//解决存在多个冒号的问题
		var command = event.data.substring(9,event.data.length);

		switch(cmd){
		case 'connect_':
			if(command == "open")//刚连接 启动成功
			{
				console.log("from worker update : "+ event.data);
				log("from worker update : open");
				workerObj_update_state = true;
			}
			else//连接失败
			{
				console.log("from worker update : "+ event.data);
				log("from worker update : close");
				workerObj_normal_state = false;
			}
			break;
		case 'getState':
			// 0使能状态on off 1运动状态run stop 2当前模式T1 3当前坐标系J W 
			// 4单步/连续step 5手动速度 6自动速度
			// console.log("from worker update : "+ event.data);

			var tmp_sp = command.split(";");

			if(tmp_sp.length>6){//此处需要补充 根据当前选择的修改显示的速度值
				data_servo = tmp_sp[0];//使能状态

				data_runstate = tmp_sp[1];//运行状态
				
				data_model = tmp_sp[2];//模式

				data_coord = tmp_sp[3];//坐标系

				data_debug = tmp_sp[4];//调试

				data_speed_t = tmp_sp[5];//速度
				data_speed_a = tmp_sp[6];
			}
			break;
		case 'nowPoint':
			//X;Y;Z;W;P;R;J1;J2;J3;J4;J5;...
			// console.log("from worker update : "+ event.data);
			var tmp_sp = command.split(";");
			if(tmp_sp.length > 6){//tmp_sp.length有时候为1 但是空数据
				for(var i = 0;i<tmp_sp.length;i++)
				{
					if(i<6)
					{
						data_point_world[i] = tmp_sp[i];
					}
					else
					{
						var j = i-6;
						if( j<7 ){
							data_point_joint[j] = tmp_sp[i];
						}
					}
				}
			}
			break;
		case 'getMsg__':
			//code;msg;code;msg...
			// console.log("from worker update msg: "+ event.data);
			data_message = command;
			break;
		case 'openFile':
			console.log("from worker update : "+ event.data);
			log("from worker update : "+ event.data);
			break;
		default:
			//return false;
		}
		
	};

	//初始化ip
	workerObj_update.postMessage(ipAddress_update); 
}
